/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_OSAPI.h
 * 该文件将Lilac OS规定的操作系统API与FreeRTOS的API接口统一，实现操作系统隔离
 * 隔离优先采用宏定义形式，将主要工作交给编译器完成，从而减少星务计算机工作量
 * 编码格式为UTF-8
 * If you see some messy code, please change the format into UTF-8
 * To Boldly Go Where No One Has Gone Before!*
 *  Created on: 2022/4/13/
 *      Author: Casa XU @ LilacSat
 */

#ifndef OS_LAYER_INC_LOS_OSAPI_H_
#define OS_LAYER_INC_LOS_OSAPI_H_

#include "FreeRTOS.h"
#include "os_task.h"
#include "os_semphr.h"

/*类型替换*/
typedef void (*LOS_TaskFunc_t)( void * );   //任务函数名
typedef unsigned long LOS_Priority_t;       //任务优先级
typedef void * LOS_TaskHandle_t;            //任务句柄（指向任务堆栈）
typedef long LOS_Base_t;                    //BaseType_t
typedef uint32_t LOS_Tick_t;                //TickType_t
typedef void * LOS_QueueHandle_t;
typedef LOS_QueueHandle_t LOS_SemaphoreHandle_t;

/*任务优先级规定*/
//注意：OS最高优先级至少设置为9
#define LOS_Task_PriorityHighest   configMAX_PRIORITIES-1
#define LOS_Task_PriorityHigher    LOS_Task_PriorityHighest-1
#define LOS_Task_PriorityHigh      LOS_Task_PriorityHigher-1
#define LOS_Task_PriorityMidHigh   LOS_Task_PriorityHigh-1
#define LOS_Task_PriorityMidMid    LOS_Task_PriorityMidHigh-1
#define LOS_Task_PriorityMidLow    LOS_Task_PriorityMidMid-1
#define LOS_Task_PriorityLow       LOS_Task_PriorityMidLow-1
#define LOS_Task_PriorityLower     LOS_Task_PriorityLow-1
#define LOS_Task_PriorityLowest    LOS_Task_PriorityLower-1


/*堆栈大小规定*/
//说明：在FreeRTOS中，一个堆栈单位为 4 Bytes
//堆栈不能设置过大，经测试，超出这一大小范围会出现BUG，TMS570的SRAM大小为512KB
//测试时，可以调用uxTaskGetStackHighWaterMark()函数查询堆栈使用情况
#define LOS_Task_Stack512B   128
#define LOS_Task_Stack1024B  256
#define LOS_Task_Stack2048B  512
#define LOS_Task_Stack4096B  1024
#define LOS_Task_Stack8192B  2048

/*任务函数名称规定*/
//函数名：Task_XXXX_Func XXXX描述了任务用途
//任务名：Task_XXXX  (一般调试时追踪任务使用，实际运行时没用)
//任务句柄：Task_XXXX_Handle



/* LOS操作系统级封装函数声明 */

LOS_Base_t LOS_Task_Create(LOS_TaskFunc_t  TaskFunc, const char * const TaskName, const uint16_t StackSize, void * const pParameters, LOS_Priority_t Priority, LOS_TaskHandle_t * const pxCreatedTask );
void LOS_Task_Delete(LOS_TaskHandle_t TaskToDelete);
void LOS_Task_Delay(const LOS_Tick_t TicksToDelay);
void LOS_Task_DelayUntil(LOS_Tick_t * const pPreviousWakeTime, const LOS_Tick_t TicksToDelay);
void LOS_Task_Suspend(LOS_TaskHandle_t TaskToSuspend);
void LOS_Task_Resume(LOS_TaskHandle_t TaskToResume);
void LOS_Task_SuspendAll();
void LOS_Task_ResumeAll();
LOS_Tick_t LOS_Task_GetTick();
void LOS_Task_Start(void);
LOS_QueueHandle_t LOS_BinSemphr_Create();
LOS_Base_t LOS_BinSemphr_Take(LOS_QueueHandle_t xSemaphore, LOS_Tick_t xBlockTime);
LOS_Base_t LOS_BinSemphr_Give(LOS_QueueHandle_t xSemaphore);
LOS_Base_t LOS_BinSemphr_GiveFromISR(LOS_QueueHandle_t xSemaphore, LOS_Base_t * const pxHigherPriorityTaskWoken);										

#endif /* OS_LAYER_INC_LOS_OSAPI_H_ */
